package com.Reporte_MasVendidos;

import java.util.List;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;

import android.graphics.Color;
import android.graphics.Paint.Align;

public class barra_linea_RepMasVendidos
{
	private XYMultipleSeriesDataset dataSet = null;
	private XYMultipleSeriesRenderer mRenderer = null;
	private List<double[]> data = null;
	private List<double[]> dataLine = null;
	private int [] coloresBarra = new int[] {Color.GREEN, Color.GRAY};
	private int [] coloresLinea = new int[] {Color.RED, Color.RED};
	private String [] nomEventos = {};
	private String titulo = "";
	
	public void execute(){ doGraph();}
		
	public void execute(List<double[]> data, List<double[]> dataLine, String[] nomEventos, String titulo)	{
		this.data = data;	
		this.dataLine = dataLine;
		this.titulo = titulo;
		this.nomEventos = nomEventos;
		doGraph();
	}
	
	private void doGraph ()
	{
		dataSet = new XYMultipleSeriesDataset();
		mRenderer = new XYMultipleSeriesRenderer();
		createDataSet();
		buildRender();
		tuneRender();
	}
	
	private void tuneRender()
	{
		mRenderer.setBarSpacing(2);
		mRenderer.setChartTitle(this.titulo);
		mRenderer.setChartTitleTextSize(30);
		
		// Sets the X axis labels color
		mRenderer.setLabelsColor(Color.WHITE);
		
		//Sets the current orientation of the chart X axis
		mRenderer.setOrientation(Orientation.HORIZONTAL);
		
		mRenderer.setShowGrid(true);
		mRenderer.setGridColor(Color.WHITE);		
		
		//Sets the approximate number of labels for the X axis
		mRenderer.setXLabels(10);
		mRenderer.setYLabels(10);
		
		mRenderer.setPanEnabled(true,true);
		//mRenderer.setMargins(new int[] {20, 100, 30, 150});
		mRenderer.setMargins(new int[] {40, 35, 0, 5}); //{arriba, derecha, abajo, izquierda}
		
		//Sets nombre de XtextLabel
		mRenderer.setLabelsTextSize(15);
		
		mRenderer.setLegendTextSize(25);
		mRenderer.setLegendHeight(70);
		
		mRenderer.setZoomRate(1.1f);
		mRenderer.setZoomButtonsVisible(true);
		
		//Sets the start value of the X axis range.
		mRenderer.setXAxisMin(0);
		mRenderer.setYAxisMin(0);
		
		//Sets the end value of the X axis range
		//mRenderer.setXAxisMax(6+1);
		//mRenderer.setYAxisMax(455+100);
				
		mRenderer.setYTitle("Cantidad");
		mRenderer.setXTitle("Eventos");
		
		mRenderer.setAxisTitleTextSize(25);
		
		mRenderer.clearXTextLabels();
			
		if (this.nomEventos.length>5){
			mRenderer.setAxisTitleTextSize(10);
			mRenderer.setLabelsTextSize(10);
			if (this.nomEventos.length>10){
				mRenderer.setAxisTitleTextSize(5);
				mRenderer.setLabelsTextSize(5);
			}
		}else
			mRenderer.setAxisTitleTextSize(25);
		
		//Centrar las barras					
			double[]yValues=data.get(0);
			
			
			if (yValues.length!=0){
			
				double maxValY, minValY;
				
				//Determinamos el mayor valor del arreglo
				maxValY = findMaxY(yValues);
						
				//Determinamos el menor valor de; arreglo
				minValY = findMinY(yValues);				
				
				mRenderer.setYAxisMax(maxValY + getIndicadorMaxY(maxValY));
				mRenderer.setYAxisMin(minValY - getIndicadorMinY(minValY));				
			}
		
			if (this.nomEventos.length!=0){
				//Etiquetas del eje X
				mRenderer.setXTitle("Eventos");
				mRenderer.setXAxisMin(0);
				mRenderer.setXAxisMax(this.nomEventos.length + 1);
				mRenderer.addXTextLabel(0,"" + (this.nomEventos[0]));
				for(int i=0;i<this.nomEventos.length;i++){
					mRenderer.addXTextLabel(i+1,""+ this.nomEventos[i]);
				}
			}
	}
	
	private void buildRender(){
		
		XYSeriesRenderer renderer = null;
		for(int i=0;i<data.size();i++)
		{
			renderer = new XYSeriesRenderer();
			renderer.setDisplayChartValues(true); //Se muestra valores
			renderer.setChartValuesSpacing(30); //Tamano de los valores
			renderer.setChartValuesTextAlign(Align.RIGHT);
			renderer.setChartValuesTextSize(35);
			renderer.setColor(coloresBarra[i]);
			
			mRenderer.addSeriesRenderer(renderer);
		}	
		
		
		//tuneo de las lineas
		for(int i=0;i<dataLine.size();i++)
		{
			renderer = new XYSeriesRenderer();
			renderer.setDisplayChartValues(true); //Se muestra valores
			renderer.setChartValuesSpacing(60);
			renderer.setChartValuesTextAlign(Align.RIGHT);
			renderer.setChartValuesTextSize(30);
			renderer.setChartValuesSpacing(8); //Espacio entre el punto y el valor
			renderer.setLineWidth(5); //Ancho de linea
			renderer.setPointStyle(PointStyle.TRIANGLE); //Punto
			renderer.setColor(coloresLinea[i]);
						
			mRenderer.addSeriesRenderer(renderer);
		}
		
	}
	
	private void createDataSet() {
		
		if (data!=null)	{
			CategorySeries serie = null;
			for (double[] object:data){
				serie = new CategorySeries("Ventas" );
				for (int i=0;i<object.length;i++){
					serie.add("Barra" + (i+1), object[i]);
				}
				dataSet.addSeries(serie.toXYSeries());
			}
		}	
		
		if(dataLine != null)
		{
			TimeSeries series = null;
			for (double[] object:dataLine)
			{
				series = new TimeSeries("");
				for (int i= 0; i < object.length; i++)
				{
					series.add(i+1,object[i]);
				}
				dataSet.addSeries(series);
			}
		}
		
	}
	
	//Encontrar el maximo de un arreglo
	public double findMaxY (double[] array){
		double max = array[0];
		for(int i=1;i<array.length;i++)
			if (array[i]>max)
				max = array[i];
		return max;
	}
	
	
	public int getIndicadorMaxY (double maxY){
		//double maxY = findMaxY(array);
		int indicador = 1;
		
		while (maxY >= 1)
		{
			maxY /= 10;
			if (maxY>=1)
			indicador *= 10;
		}
		return indicador;
	}
	
	public int getIndicadorMinY (double minY){
		//double minY = findMinY(array);
		int indicador = 1;
		
		while (minY >= 1)
		{
			minY /= 10;
			if (minY>=1)
			indicador *= 10;
		}
		return indicador;
	}
	
	//Encontrar el minimo de un arreglo
			public double findMinY (double[] array){
				double min = array[0];
				for(int i=1;i<array.length;i++)
					if (array[i]<min)
						min = array[i];
				return min;
			}

	/************************* GET/SET ***************************/
	public XYMultipleSeriesDataset getDataSet() {return dataSet;}
	public void setDataSet(XYMultipleSeriesDataset dataSet) {this.dataSet = dataSet;}
	public XYMultipleSeriesRenderer getmRenderer() {return mRenderer;}
	public void setmRenderer(XYMultipleSeriesRenderer mRenderer) {this.mRenderer = mRenderer;}
	public List<double[]> getData() {return data;}
	public void setData(List<double[]> data) {this.data = data;}
	public List<double[]> getDataLine() {return dataLine;}
	public void setDataLine(List<double[]> dataLine) {this.dataLine = dataLine;}
}
